<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Class Model_Finance_Record
 */
class Model_Finance_Record extends ORM
{


    /**
     *
     * @param $amount 交易金额
     * @param $type 类型 1,收入 2,支出
     * @param $type_desc 收支类型描述
     * @param string $remark 备注
     * @param string $ordersn 此次交易涉及的订单
     * @return mixed
     */
    public static function add_finance_record($amount,$type,$type_desc,$remark='',$ordersn='')
    {
        $data = array(
            'amount' => $amount,
            'type' => $type,
            'type_desc' => $type_desc,
            'ordersn' => $ordersn,
            'remark' => $remark,
            'addtime' => time()
        );
        if($ordersn)
        {
            //如果已记录相同记录,则不在记录
            $row = DB::select()->from('finance_record')
                ->where('ordersn','=',$ordersn)
                ->and_where('type','=',$type)
                ->execute()->current();
            if($row['id'])
            {
                return true;
            }

        }
        DB::insert('finance_record',array_keys($data))->values(array_values($data))->execute();
    }


}